package com.mib.util;

/**
 * @Author: TWC
 * @createTime: 2024年04月24日 00:39:12
 * @Description: TODO
 */
public class TextFormatUtil {

    public static String getChinese(String octetString) {
        if(octetString.contains(":")){
            try {
                String[] temps = octetString.split(":");
                if(temps.length == 6){
                    return octetString;
                }else{
                    byte[] bs = new byte[temps.length];
                    for (int i = 0; i < temps.length; i++) {
                        bs[i] = (byte) Integer.parseInt(temps[i], 16);
                    }
                    if(bs[temps.length - 1] == 0){
                        bs = removeLastElement(bs);
                    }
                    return new String(bs, "GB2312");
                }
            } catch (Exception e) {
                return octetString;
            }
        }
        return octetString;
    }

    public static byte[] removeLastElement(byte[] original) {
        if (original == null || original.length == 0) {
            throw new IllegalArgumentException("The array is empty or null");
        }
        byte[] resized = new byte[original.length - 1];
        System.arraycopy(original, 0, resized, 0, original.length - 1);
        return resized;
    }

}
